import 'package:farmer_app/widgets/pages/work_space/work_equipment_component.dart';
import 'package:farmer_app/widgets/pages/work_space/work_record_component.dart';
import 'package:flutter/material.dart';

import '../../../common/app_theme.dart';
import 'card_header_component.dart';

class WorkSpacePage extends StatefulWidget {

  final int workSpaceId;
  final AnimationController? animationController;

  const WorkSpacePage({Key? key, required this.workSpaceId, this.animationController}) : super(key: key);

  @override
  State<WorkSpacePage> createState() => _WorkSpacePageState();
}

class _WorkSpacePageState extends State<WorkSpacePage> with TickerProviderStateMixin{

  Animation<double>? topBarAnimation;
  final ScrollController scrollController = ScrollController();
  double topBarOpacity = 1.0;
  late TabController _tabController;
  @override
  void initState() {
    topBarAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(
        CurvedAnimation(
            parent: widget.animationController!,
            curve: Interval(0, 0.5, curve: Curves.fastOutSlowIn)));

    // scrollController.addListener(() {
    //   if (scrollController.offset >= 24) {
    //     if (topBarOpacity != 1.0) {
    //       setState(() {
    //         topBarOpacity = 1.0;
    //       });
    //     }
    //   } else if (scrollController.offset <= 24 &&
    //       scrollController.offset >= 0) {
    //     if (topBarOpacity != scrollController.offset / 24) {
    //       setState(() {
    //         topBarOpacity = scrollController.offset / 24;
    //       });
    //     }
    //   } else if (scrollController.offset <= 0) {
    //     if (topBarOpacity != 0.0) {
    //       setState(() {
    //         topBarOpacity = 0.0;
    //       });
    //     }
    //   }
    // });

    _tabController = TabController(length: 2, vsync: this);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return  Container(
      color: AppTheme.background,
      child: Scaffold(
        backgroundColor: Colors.transparent,
        body: NestedScrollView(
          controller: scrollController,
          body: getMainListViewUI(),
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return <Widget>[
              getAppBarUI()
            ];
          },
        ),
      ),
    );
  }

  // 主页
  Widget getMainListViewUI() {
    return TabBarView(
      controller: _tabController,
        children: [
          WorkEquipmentComponent(workSpaceId: widget.workSpaceId, scrollController: this.scrollController, animationController: widget.animationController,),
          WorkRecordComponent(workSpaceId: widget.workSpaceId, scrollController: this.scrollController, animationController: widget.animationController,),


    ]);
  }
  // appBar
  SliverAppBar getAppBarUI() {
    return SliverAppBar(
      pinned: true,
      title: Opacity(
        opacity: topBarOpacity,
        child: Text('1号大棚', style: TextStyle(color: AppTheme.darkText),),
      ),
      expandedHeight: 340,
      flexibleSpace: FlexibleSpaceBar(
        background: Container(
          height: 340,
          color: AppTheme.background,
          child: Column(
            children: [
              SizedBox(height: MediaQuery.of(context).padding.top + 54,),
              CardHeaderComponent(
                animation: Tween<double>(begin: 0.0, end: 1.0).animate(CurvedAnimation(
                    parent: widget.animationController!,
                    curve:
                    Interval(1, 1.0, curve: Curves.fastOutSlowIn))),
                animationController: widget.animationController!,
              ),
              SizedBox(height: 10,)
            ],
          ),
        ),
      ),
      bottom: PreferredSize(
        preferredSize: Size(MediaQuery.of(context).size.width, 44),
        child: Material(
          color: AppTheme.nearlyWhite,
          child: TabBar(
              controller: _tabController,
              tabs: [
                Tab(text: '设备'),
                Tab(text: '记录')
              ]
          ),
        ),
      )
    );
  }

}
